package org.csu.measure.service;

import org.csu.measure.domain.*;
import org.csu.measure.domain.Class;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class CKService extends DefaultHandler {
    private CKMetrics ck;
    private Class targetClass;
    private List<Class> lists;
    private String methodString;

    public CKService(Class targetClass, List<Class> lists) {
        super();
        ck = new CKMetrics();
        this.targetClass = targetClass;
        this.lists = lists;
    }

    public CKService(Class targetClass, List<Class> lists, String methodString) {
        super();
        ck = new CKMetrics();
        this.targetClass = targetClass;
        this.lists = lists;
        this.methodString = methodString;
    }

    public CKService() {
        ck = new CKMetrics();
    }

    /** Weighted Method per class   类加权方法数*/
    public double getWMC(){
        return targetClass.getOperations().size();
    }

    /** Response For a Class    类的响应数量*/
    public double getRFC(){
        return targetClass.getAssociations().size() + targetClass.getDependencies().size() + targetClass.getOperations().size();
    }

    /** Depth of Inheritance Tree   继承树的深度*/
    public double getDIT(){
        int DIT = 0;
        Class parent = targetClass.getFather();
        while (parent != null) {
            DIT++;
            parent = parent.getFather();
        }
        return DIT;
    }

    /** Number Of Children      子类数量*/
    public double getNOC(){
        if (targetClass.getChildren() != null) {
            return targetClass.getChildren().size();
        } else {
            return 0;
        }
    }

    /** Coupling Between Objects       对象间的耦合度*/
    public double getCBO(){
        int CBO = 0;
        if (targetClass.getAssociations() != null) {
            CBO += targetClass.getAssociations().size();
        }
        if (targetClass.getDependencies() != null) {
            CBO += targetClass.getDependencies().size();
        }
        return CBO;
    }

    /** Lack of Cohesion        类缺乏内聚性*/
    public double getLCOM(){
        int LCOM = 0;   //初始化
        List<Operation> operations = targetClass.getOperations();
        if(operations.size() >= 2){

            for (int i = 0; i < operations.size(); i++){
                List<Parameter> parameters = operations.get(i).getParameters();
                for(int j = i + 1; j < operations.size(); j++){
                    List<Parameter> parameters1 = operations.get(j).getParameters();
                    if(parameters1 != null){
                        for (Parameter parameter:parameters1
                        ) {

                            for (Parameter p:parameters
                            ) {
                                if (p.getName().equals(parameter.getName())){

                                    List<AttributeOfClass> attribute = targetClass.getAttributes();
                                    for (AttributeOfClass a:attribute
                                    ) {
                                        if(a.getName().equals(parameter.getName())){
                                            LCOM++;
                                        }
                                    }


                                }
                            }

                        }
                    }


                }
            }
        }
        return LCOM;
    }

    public CKMetrics getCk(Class targetClass){
        ck.setName(targetClass.getName());
        ck.setWMC(getWMC());
        ck.setRFC(getRFC());
        ck.setCBO(getCBO());
        ck.setDIT(getDIT());
        ck.setNOC(getNOC());
        ck.setLCOM(getLCOM());
        return ck;
    }
}
